package info.itloser.pltv.ui

import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import info.itloser.pltv.R
import info.itloser.pltv.adapter.ChannelAdapter
import info.itloser.pltv.adapter.ResAdapter
import info.itloser.pltv.bean.ResItem
import info.itloser.pltv.bean.TvItem
import info.itloser.pltv.databinding.ActivityPltvBinding
import info.itloser.pltv.utils.PLtvUtil

class PLtvActivity : AppCompatActivity() {

    //这应该是懒加载
    private val tvViewModel by lazy { ViewModelProvider(this).get(PLtvViewModel::class.java) }

    private lateinit var tvBinding: ActivityPltvBinding
    private lateinit var adapter: ChannelAdapter

    private lateinit var resAdapter: ResAdapter

    //请求到的数据
    private lateinit var tvItem: TvItem

    private lateinit var resItems: ArrayList<ResItem>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //初始化 DataBind 布局
        tvBinding = DataBindingUtil.setContentView(this, R.layout.activity_pltv)

        //初始化沉浸式状态栏 这个东西暂时会导致抽屉打不开 先禁用 最后再解决
//        PLtvUtil.setTopAndBottomTransparentAndTextBlack(this)

        //设置生命周期
        tvBinding.lifecycleOwner = this
        //将 ViewModel 绑定给 xml布局里申明的 xmlViewModel (类似于赋值 将 tvViewModel赋值给 xml中声明的xmlViewModel 它俩本质是一个对象)
        tvBinding.xmlViewModel = tvViewModel

        //监听准备就绪
        tvBinding.videoTv.setOnPreparedListener {
            //开始播放
            tvBinding.videoTv.start()

        }

        //监听播放完毕
        tvBinding.videoTv.setOnCompletionListener {

        }

        //监听播放异常
        tvBinding.videoTv.setOnErrorListener { mp, what, extra ->

            Log.i("itl-播放异常", "mp: $mp\twhat: $what\textra: $extra")

            //直接切换到下一个源

            true
        }

        //获取初始化数据
        channelsToViewModel()

        //初始化适配器
        adapter = ChannelAdapter(tvItem.channelList, this)
        tvBinding.rvChannelList.layoutManager = LinearLayoutManager(this)
        tvBinding.rvChannelList.adapter = adapter

        //初始化 源适配器
        resItems = ArrayList()
        resItems.addAll(tvItem.channelList[0].resList)
        resAdapter = ResAdapter(resItems, this)
        tvBinding.rvResList.layoutManager = LinearLayoutManager(this)
        tvBinding.rvResList.adapter = resAdapter

        //adapter item点击事件
        // 这里 object 写法参照 https://blog.csdn.net/weixin_42708161/article/details/107919566
        // kotlin写法 真TM学到了 折磨半天了 NND
        adapter.onClick = object : ChannelAdapter.OnClick {
            override fun onItemClick(position: Int) {

                Toast.makeText(
                    this@PLtvActivity,
                    "正在播放: " + tvItem.channelList[position].channelName,
                    Toast.LENGTH_SHORT
                ).show()

                //切换源的数据
                resItems.clear()
                resItems.addAll(tvItem.channelList[position].resList)
                resItems[0].isCheck = true//默认选中源0
                resAdapter.notifyDataSetChanged()

                //切换播放地址
                tvBinding.videoTv.setVideoURI(Uri.parse(resItems[0].resUrl))

                //切换多个源


            }

        }

        resAdapter.onClick = object : ResAdapter.OnClick {

            override fun onItemClick(position: Int) {

                Toast.makeText(
                    this@PLtvActivity,
                    "切换到源: " + resItems[position].resName,
                    Toast.LENGTH_SHORT
                ).show()

                //切换播放地址
                tvBinding.videoTv.setVideoURI(Uri.parse(resItems[position].resUrl))
            }
        }

    }

    /**
     * 从静态资源库获取 json 数据 并传递给 ViewModel
     */
    private fun channelsToViewModel() {

        //直播源解析完成
        val jsonString = PLtvUtil.readStringFromAssets(this, "cctv_index.json")

        //用Gson解析数据
        tvItem = PLtvUtil.gsonToBean(jsonString, TvItem::class.java)
        Log.i("itl-解析到", "频道数: " + tvItem.channelList.size)
        for (i in 0 until tvItem.channelList.size)
            Log.i("itl-解析到", tvItem.channelList[i].channelName)

        //推给 ViewModel


    }


}

